//
//  UI_Closure_VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/7/23.
//

import UIKit

class UI_Closure_VC: UIViewController {

    var sumClosure = {
        (num1: Int, num2: Int) -> Int in
        return num1 + num2
        
    }
    
    func sumFunc(num1: Int, num2: Int) -> Int {
        num1 + num2
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
//        let sum1 = sumClosure(10, 20)
//        let sum2 = sumFunc(num1: 10, num2: 20)
//        debugPrint("sum1 is \(sum1), sum2 is \(sum2)")
        
        /// "sum1 is 30, sum2 is 30"
        
        testFn()
    }

}

extension UI_Closure_VC {
    typealias Fn = (Int) -> Int
    
    private func getFn() -> Fn {
        var num = 0
        func plus(_ i: Int) -> Int {
            num += i
            return num
        }
        return plus
    }
    
    private func testFn() {
        let fn = getFn()
        print(fn(1))
        print(fn(2))
        print(fn(3))
        print(fn(4))
    }
    /**
     Log:
     1
     3
     6
     10
     */
}

